package com.cn.kabalifeuser.ui.integral.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.LazyFragment;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.ui.integral.activity.ActSpecialAreaActivity;
import com.cn.kabalifeuser.ui.integral.activity.IntegralCommodityDetailsActivity;
import com.cn.kabalifeuser.ui.integral.adapter.ShoppingMallAdapter;
import com.cn.kabalifeuser.ui.integral.bean.BannerActBean;
import com.cn.kabalifeuser.ui.integral.bean.ShoppingMalListBean;
import com.cn.kabalifeuser.ui.service.activity.RichTextH5Activity;
import com.cn.kabalifeuser.ui.service.activity.RichTextWebViewH5Activity;
import com.cn.kabalifeuser.ui.service.bean.BannerListBean;
import com.cn.kabalifeuser.utils.GlideImageLoader;
import com.cn.kabalifeuser.utils.RxToast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 晓林 on 2020/9/10.
 * function: 精选
 */
public class SelectedFragments extends LazyFragment<ShoppingMallView, ShoppingMallPresenter> implements ShoppingMallView {
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.banner_act)
    Banner bannerAct;
    @BindView(R.id.rv_notice)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    BannerListBean bannerListBean;
    BannerActBean bannerActBean;


    List<ShoppingMalListBean.ResultBean.DataBean> mList;
    ShoppingMallAdapter mAdapter;

    private int mId;

    public static SelectedFragments getInstance(int mid) {
        SelectedFragments fragment = new SelectedFragments();
        fragment.mId = mid;
        return fragment;
    }

    @Override
    protected ShoppingMallPresenter createPresenter() {
        return new ShoppingMallPresenter();
    }

    @Override
    protected void onUserVisible() {
        if (mList != null && mList.size() == 0) {
            pagehttp = 1;
            getListData();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_shopping_selected;
    }


    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);
        mList = new ArrayList<>();
        recyclerView.setLayoutManager(new GridLayoutManager(getMe(), 2));
        recyclerView.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new ShoppingMallAdapter(R.layout.fragment_mall_item, mList, 1);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //todo 跳转详情
                Bundle bundle = new Bundle();
                bundle.putInt("id", mAdapter.getItem(position).getId());
                $startActivity(IntegralCommodityDetailsActivity.class, bundle);
            }
        });
        initRefreshLayout();

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (bannerListBean.getResult().get(position).getType() == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", Integer.parseInt(bannerListBean.getResult().get(position).getParameter()));
                    $startActivity(IntegralCommodityDetailsActivity.class, bundle);
                } else if (bannerListBean.getResult().get(position).getType() == 2) {
                    Bundle bundle = new Bundle();
                    bundle.putString("title", "轮播图活动");
                    bundle.putString("content", bannerListBean.getResult().get(position).getParameter());
                    $startActivity(RichTextH5Activity.class, bundle);
                } else if (bannerListBean.getResult().get(position).getType() == 3) {
                    Bundle bundle = new Bundle();
                    bundle.putString("url", bannerListBean.getResult().get(position).getParameter());
                    $startActivity(RichTextWebViewH5Activity.class, bundle);
                }
            }
        });
        bannerAct.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", bannerActBean.getResult().getData().get(position));
                $startActivity(ActSpecialAreaActivity.class, bundle);
            }
        });

        //top 轮播
        mPresenter.onBannerData(LoginHelper.getLonginData().getResult(), 3);

        //top  活动 轮播
        mPresenter.onBannerActData(LoginHelper.getLonginData().getResult());
    }

    /**
     * 刷新
     */
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    private void getListData() {
        mPresenter.onShoppingMallData(LoginHelper.getLonginData().getResult(), "", "2", "", pagehttp, pageNum);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onBannerActSuccess(BannerActBean bean) {
        bannerActBean = bean;
        List<String> listBanner = new ArrayList<>();
        for (int i = 0; i < bean.getResult().getData().size(); i++) {
            listBanner.add(bean.getResult().getData().get(i).getCover());
        }
        bannerAct.setImageLoader(new GlideImageLoader());
        //设置图片集合
        bannerAct.setImages(listBanner);
        //动画效果
        //banner.setBannerAnimation(Transformer.Accordion);
        //banner设置方法全部调用完毕时最后调用
        bannerAct.start();
    }

    @Override
    public void onBannerSuccess(BannerListBean bean) {
        bannerListBean = bean;
        List<String> listBanner = new ArrayList<>();
        for (int i = 0; i < bean.getResult().size(); i++) {
            listBanner.add(bean.getResult().get(i).getBanner());
        }
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(listBanner);
        //动画效果
        //banner.setBannerAnimation(Transformer.Accordion);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    @Override
    public void onShoppingMalListSuccess(ShoppingMalListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }
}

